using Microsoft.AspNetCore.Mvc;
using MediatR;
using SupplierCustomerManagement.Read.API.Dtos;
using SupplierCustomerManagement.Read.API.Dtos.Lambing;
using SupplierCustomerManagement.Read.API.Applications.Commands.lambing;

namespace SupplierCustomerManagement.Read.API.Controllers
{
    /// <summary>
    /// 产羔统计
    /// </summary>
    [ApiController]
    [Route("api/lambing")]
    public class LambingController : ControllerBase
    {
        private readonly ILogger<LambingController> _logger;
        private readonly IMediator _mediator;

        public LambingController(ILogger<LambingController> logger, IMediator mediator)
        {
            _logger = logger;
            _mediator = mediator;
        }

        /// <summary>
        /// 获取产羔月度统计
        /// </summary>
        [HttpGet("monthly-stats")]
        public async Task<ActionResult<ApiResponse<List<LambingMonthlyRowDto>>>> GetMonthlyStats([FromQuery] string year, [FromQuery] string? month, [FromQuery] int? farmId)
        {
            try
            {
                var data = await _mediator.Send(new GetLambingMonthlyStatsQuery
                {
                    year = year,
                    month = month,
                    farmId = farmId
                });
                return Ok(ApiResponse<List<LambingMonthlyRowDto>>.Ok(data));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取产羔月度统计失败");
                return StatusCode(500, ApiResponse<List<LambingMonthlyRowDto>>.Fail($"获取产羔月度统计失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 窝均产活羔统计
        /// </summary>
        [HttpGet("average-live")]
        public async Task<ActionResult<ApiResponse<List<AverageLivePointDto>>>> GetAverageLive([FromQuery] string start, [FromQuery] string end, [FromQuery] int? farmId)
        {
            try
            {
                var data = await _mediator.Send(new GetAverageLiveStatsQuery
                {
                    start = start,
                    end = end,
                    farmId = farmId
                });
                return Ok(ApiResponse<List<AverageLivePointDto>>.Ok(data));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取窝均产活羔统计失败");
                return StatusCode(500, ApiResponse<List<AverageLivePointDto>>.Fail($"获取窝均产活羔统计失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 胎次产羔结构统计
        /// </summary>
        [HttpGet("parity-stats")]
        public async Task<ActionResult<ApiResponse<List<ParityStatsPointDto>>>> GetParityStats([FromQuery] string year, [FromQuery] int? farmId)
        {
            try
            {
                var data = await _mediator.Send(new GetParityStructureStatsQuery
                {
                    year = year,
                    farmId = farmId
                });
                return Ok(ApiResponse<List<ParityStatsPointDto>>.Ok(data));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取胎次产羔结构统计失败");
                return StatusCode(500, ApiResponse<List<ParityStatsPointDto>>.Fail($"获取胎次产羔结构统计失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 产羔哺乳-月度统计
        /// </summary>
        [HttpGet("lactation-monthly-stats")]
        public async Task<ActionResult<ApiResponse<List<LactationMonthlyStatsDto>>>> GetLactationMonthlyStats([FromQuery] string year, [FromQuery] int? farmId)
        {
            try
            {
                var data = await _mediator.Send(new GetLactationMonthlyStatsQuery
                {
                    year = year,
                    farmId = farmId
                });
                return Ok(ApiResponse<List<LactationMonthlyStatsDto>>.Ok(data));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取产羔哺乳月度统计失败");
                return StatusCode(500, ApiResponse<List<LactationMonthlyStatsDto>>.Fail($"获取产羔哺乳月度统计失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 羊场选项（基于分娩记录去重 FarmId）
        /// </summary>
        [HttpGet("farms")]
        public async Task<ActionResult<ApiResponse<List<FarmOptionDto>>>> GetFarms()
        {
            try
            {
                var data = await _mediator.Send(new GetFarmOptionsQuery());
                return Ok(ApiResponse<List<FarmOptionDto>>.Ok(data));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取羊场列表失败");
                return StatusCode(500, ApiResponse<List<FarmOptionDto>>.Fail($"获取羊场列表失败: {ex.Message}"));
            }
        }
    }
}
